using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace LS.Setting
{
    public partial class ApplicationSetting
    {
        [System.Serializable]
        public class VideoSetting : SettingBase
        {
            private ModifiableValue<FullScreenMode> m_DisplayMode;
            private ModifiableValue<Resolution> m_DisplayResolution;
            private ModifiableValue<VideoQualityType> m_VideoQuality;
            private ModifiableValue<TextureQualityType> m_TextureQuality;
            private ModifiableValue<EffectsQualityType> m_EffectsQuality;

            /// <summary>
            /// 全屏模式状态
            /// </summary>
            public FullScreenMode DisplayMode
            {
                get
                {
                    return m_DisplayMode.Value;
                }

                set
                {
                    m_DisplayMode.SetValue(value);
                }
            }

            /// <summary>
            /// 分辨率设置
            /// </summary>
            public Resolution DisplayResolution
            {
                get
                {
                    return m_DisplayResolution.Value;
                }
                set
                {
                    m_DisplayResolution.SetValue(value);
                }
            }

            /// <summary>
            /// 画面质量
            /// </summary>
            public VideoQualityType VideoQuality
            {
                get
                {
                    return m_VideoQuality.Value;
                }
                set
                {
                    m_VideoQuality.SetValue(value);
                }
            }

            /// <summary>
            /// 贴图质量
            /// </summary>
            public TextureQualityType TextureQuality
            {
                get
                {
                    return m_TextureQuality.Value;
                }
                set
                {
                    m_TextureQuality.SetValue(value);
                }
            }

            /// <summary>
            /// 特效质量
            /// </summary>
            public EffectsQualityType EffectsQuality
            {
                get
                {
                    return m_EffectsQuality.Value;
                }
                set
                {
                    m_EffectsQuality.SetValue(value);
                }
            }


            /// <summary>
            /// 是否有值被修改
            /// </summary>
            public override bool IsValueChanged
            {
                get
                {
                    return m_DisplayMode.IsValueChanged ||
                            m_DisplayResolution.IsValueChanged ||
                            m_VideoQuality.IsValueChanged ||
                            m_TextureQuality.IsValueChanged ||
                            m_EffectsQuality.IsValueChanged;
                }
            }

            public VideoSetting()
            {
                Load();
            }

            /// <summary>
            /// 获取所有受支持的分辨率设置
            /// </summary>
            /// <returns></returns>
            public Resolution[] GetAllSupportedResolutions()
            {
                return Screen.resolutions;
            }

            public override void Load()
            {
                SettingComponent setting = LSEntry.Setting;

                m_DisplayMode = (ModifiableValue<FullScreenMode>)Screen.fullScreenMode;
                m_DisplayResolution = (ModifiableValue<Resolution>)Screen.currentResolution;
                if (setting != null)
                {
                    m_TextureQuality = (ModifiableValue<TextureQualityType>)(TextureQualityType)(setting.GetInt("Video.TextureQuality"));
                    m_VideoQuality = (ModifiableValue<VideoQualityType>)(VideoQualityType)(setting.GetInt("Video.VideoQuality"));
                    m_EffectsQuality = (ModifiableValue<EffectsQualityType>)(EffectsQualityType)(setting.GetInt("Video.EffectsQuality"));
                }
            }

            public override void Save()
            {

                SettingComponent setting = LSEntry.Setting;

                if (m_DisplayMode.IsValueChanged || m_DisplayResolution.IsValueChanged)
                {
                    if (!Application.isEditor)
                    {
                        Resolution resolution = m_DisplayResolution.GetValueAndClearChangedState();
                        Screen.SetResolution(resolution.width, resolution.height, m_DisplayMode.GetValueAndClearChangedState(), resolution.refreshRateRatio);
                    }
                }

                if (setting != null)
                {
                    if (m_TextureQuality.IsValueChanged)
                    {
                        setting.SetInt("Video.TextureQuality", (int)m_TextureQuality.GetValueAndClearChangedState());
                    }

                    if (m_VideoQuality.IsValueChanged)
                    {
                        setting.SetInt("Video.VideoQuality", (int)m_VideoQuality.GetValueAndClearChangedState());
                    }

                    if (m_EffectsQuality.IsValueChanged)
                    {
                        setting.SetInt("Video.EffectsQuality", (int)m_EffectsQuality.GetValueAndClearChangedState());
                    }
                }
            }
        }
    }
}
